home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / pdc / libsrc / misc / cli_parse.c next >
Encoding:
C/C++ Source or Header  |  1990-04-07  |  2.7 KB  |  121 lines

  1. /*
  2.  * PDC I/O Library Copyright (C) 1987 by J.A. Lydiatt.
  3.  * Freely Distributable for non-commercial use.
  4.  */
  5. /*
  6.  *  _main() calls this routine to parse the command line passed from
  7.  *  the CLI.  If your program does't need parameters, you can substitute
  8.  *  a dummy _cli_parse() routine such as:
  9.  *
  10.  *      "_cli_parse { return; }"
  11.  * 
  12.  *  in place of this one - makes for a shorter program!
  13.  *
  14.  *  This routine fills in the global variables:-
  15.  * 
  16.  *  _argc        - the number of arguments.
  17.  *  _argv        - array of pointers to argument strings.
  18.  *  _argBuf        - buffer to store arguments (allocated here).
  19.  *  _argBufSize    - size of the buffer allocated. 
  20.  *
  21.  *  Any storage allocated here will be freed by _exit().
  22.  * 
  23.  */
  24.  
  25. /*
  26.  * Maintenance Notes:
  27.  * 10Apr88 - (Jal) Note that AmigaDos seems to strip out double "" marks
  28.  *           so although this routine should handle them (I've no way of
  29.  *           testing it), it doesn't!
  30.  */
  31.  
  32. #define iswhite(c)  ( c == ' ' || c == '\t' )
  33. #include <libraries/dosextens.h>
  34.  
  35. typedef struct Process PROCESS;
  36. typedef struct CommandLineInterface CLI;
  37.  
  38. extern int _argc, _argBufSize;
  39. extern char **_argv, *_argBuf;
  40.  
  41. _cli_parse(tp, cmdlen, cmdptr)
  42. PROCESS *tp;            /* pointer to task process */
  43. long cmdlen;            /* length of command line  */
  44. char *cmdptr;            /* pointer to AmigaDos command line */ 
  45. {
  46.     char *s, *argBufptr, delim;
  47.     CLI *cli;
  48.     int c;
  49.     extern void *AllocMem();
  50.  
  51.     /*
  52.      * Allocate argBuff; allow space for the command's name.
  53.      */
  54.  
  55.     cli = (long)tp->pr_CLI << 2;
  56.     s = (char *)( (long)cli->cli_CommandName << 2);
  57.     _argBufSize = s[0] + cmdlen + 2;
  58.     if ( (_argBuf = AllocMem( (long)_argBufSize, 0L)) == NULL )
  59.         return;
  60.  
  61.  
  62.     strncpy(_argBuf, &s[1], s[0]);
  63.     strcpy(_argBuf + s[0], " ");
  64.     strncat(_argBuf, cmdptr, (int)cmdlen);
  65.  
  66.     /*
  67.      * Parse the augmented command line.
  68.      */
  69.  
  70.     _argc = 0;
  71.     argBufptr = _argBuf;
  72.     for ( s=_argBuf; *s ; ++_argc) {
  73.         while ( (c = *s) && iswhite(c) )
  74.             ++s;
  75.  
  76.         if ( *s < '\x1B' )
  77.             break;
  78.  
  79.         /*
  80.          * If in quote mode, just copy everything up to the ending quote.
  81.          * A double quote is interpreted as a single quote.
  82.          */
  83.  
  84.         if ( *s == '"' || *s == '\'' ) {
  85.             delim = *s++;
  86.             while ( *s ) {
  87.                 while ( *s == delim && s[1] == delim ) {
  88.                     *argBufptr++ = delim;
  89.                     s += 2;
  90.                 }
  91.                 if ( *s == delim ) {
  92.                     ++s;
  93.                     break;
  94.                 }
  95.                 while ( *s && *s != delim ) {
  96.                     *argBufptr++ = *s;
  97.                     ++s;
  98.                 }
  99.             }
  100.         } else {
  101.              while ( (c = *s++) && !iswhite(c) )
  102.                 *argBufptr++ = c;
  103.  
  104.         }
  105.         *argBufptr++ = '\0';
  106.     }    /* end for */
  107.  
  108.     *argBufptr = '\0';
  109.     if ( (_argv = AllocMem((long)(_argc+1)*sizeof(*_argv), 0L)) == NULL ) {
  110.         _argc = 0;
  111.         return;
  112.     }
  113.  
  114.     s = _argBuf;
  115.     for (c=0; c<_argc; ++c) {
  116.         _argv[c] = s;
  117.         s += strlen(s) + 1;
  118.     }
  119.     _argv[c] = 0;
  120. }
  121.